﻿using DLL.NET.DB.MS.Drugs.CRUD.Repository;
using DLL.NET50.DB.MS.Entity;
using DLL.Standard.Infrastructure.Log;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace DLL.NET.DB.MS.Drugs.CRUD.Service
{
    public interface IMSUpAndDownDrugService
    {
        bool LocalBackupToMS();

        bool MSToLocalBackup();

        //bool CopyedToLocalBackup();

        //bool RemoteBackupToLocalBackup();

        //bool LocalBackupToRemoteBackup();
    }

    public class CMSUpAndDownDrugService : IMSUpAndDownDrugService
    {
        protected IMSUpOrDownDrugRepository _repository;

        public CMSUpAndDownDrugService(IMSUpOrDownDrugRepository repository) //: base(unitOfWork, repository)
        {
            this._repository = repository;
        }

        /// <summary>
        /// 采用Socket方式，测试服务器连接
        /// </summary>
        /// <param name="host">服务器主机名或IP</param>
        /// <param name="port">端口号</param>
        /// <param name="millisecondsTimeout">等待时间：毫秒</param>
        /// <returns></returns>
        public static bool TestConnection(string host, int port, int millisecondsTimeout)
        {
            TcpClient client = new TcpClient();
            try
            {
                var ar = client.BeginConnect(host, port, null, null);
                ar.AsyncWaitHandle.WaitOne(millisecondsTimeout);
                return client.Connected;
            }
            catch (Exception e)
            {
                LogHelper.Error(e.Message);
                return false;                 //throw e;
            }
            finally
            {
                client.Close();
            }
        }

        public bool LocalBackupToMS()
        {
            return _repository.LocalBackupToMS();
        }

        public bool MSToLocalBackup()
        {
            return _repository.MSToLocalBackup();
        }

        //public bool CopyedToLocalBackup()
        //{
        //    return _repository.CopyedToLocalBackup();
        //}

        //public bool RemoteBackupToLocalBackup()
        //{
        //    return _repository.RemoteBackupToLocalBackup();
        //}

        //public bool LocalBackupToRemoteBackup()
        //{
        //    return _repository.LocalBackupToRemoteBackup();
        //}
    }
}